package com.kuaifawu.lwnlawyerclient.Fragment;

import android.content.Intent;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import com.kuaifawu.lwnlawyerclient.Activity.LWNLoginActivity;
import com.kuaifawu.lwnlawyerclient.Activity.LWNQuestionDetailActivity;
import com.kuaifawu.lwnlawyerclient.Activity.MainActivity;
import com.kuaifawu.lwnlawyerclient.Adapter.LWNAdapter_home;
import com.kuaifawu.lwnlawyerclient.Adapter.LWNAdapter_myGoodAt;
import com.kuaifawu.lwnlawyerclient.Lib.LWNJsonToData;
import com.kuaifawu.lwnlawyerclient.LWNNetwork.LWNNetworkCenter;
import com.kuaifawu.lwnlawyerclient.Lib.ToastView_custom;
import com.kuaifawu.lwnlawyerclient.Model.LWNModel_myGoodAtList;
import com.kuaifawu.lwnlawyerclient.Model.LWNModel_user;
import com.kuaifawu.lwnlawyerclient.R;
import com.kuaifawu.lwnlawyerclient.Lib.LWNSwip.LWSwipeRefreshLayout;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class KWNHomeFragment extends Fragment implements LWSwipeRefreshLayout.OnLoadListener,LWSwipeRefreshLayout.OnRefreshListener {

    @ViewInject(R.id.cursor)
    private ImageView cursor;// 动画图片
    @ViewInject(R.id.wait)
    private TextView textView_wait;
    @ViewInject(R.id.already)
    private TextView textView_already;// 页卡头标
    @ViewInject(R.id.viewpager_home)
    private ViewPager pagerView_home=null;

    private ListView listView_already;
    private ListView listView_wait;

    private List<View> listViews; // Tab页面列表
    LayoutInflater mInflater;
    private int currIndex = 0;// 当前页卡编号
    private ArrayList<LWNModel_myGoodAtList> array_myGoodAt, array_all;
    private MainActivity activity_self;

    private int one_two = 0;// 页卡1 -> 页卡2 偏移量
    private  ArrayList<LWNModel_myGoodAtList> array_AllmyGoodAt, array_Alltotal;
    private View view_wait = null;
    private View view_already = null;
    private int start=0;
    private int displaynum=20;

    public KWNHomeFragment(){

    }

    public void LoadDataWithActivity(MainActivity activity_main){
        activity_self = activity_main;

        if(one_two == 0) {
            DisplayMetrics dm = new DisplayMetrics();
            activity_self.getWindowManager().getDefaultDisplay().getMetrics(dm);
            one_two = dm.widthPixels / 2;// 页卡1 -> 页卡2 偏移量
        }

       getMyGoodAtListFromNetwork();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mInflater = inflater;
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        ViewUtils.inject(this,rootView);
        InitImageView();
        InitViewPager();
        InitTextView();
        setRefresh();

        return rootView;
    }
    private LWSwipeRefreshLayout home_alldata_refresh;
    private LWSwipeRefreshLayout home_gooddata_refresh;

    private LWSwipeRefreshLayout nulldata_homemy;
    private LWSwipeRefreshLayout nulldata_homeother;



    private void setRefresh(){
        home_gooddata_refresh = (LWSwipeRefreshLayout) view_wait.findViewById(R.id.home_gooddata_refresh);
        home_gooddata_refresh.setColor(android.R.color.holo_blue_light,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        home_gooddata_refresh.setMode(LWSwipeRefreshLayout.Mode.BOTH);
        home_gooddata_refresh.setLoadNoFull(false);
        home_gooddata_refresh.setOnRefreshListener(this);
        home_gooddata_refresh.setOnLoadListener(this);
        home_gooddata_refresh.setRefreshing(true);


        nulldata_homemy = (LWSwipeRefreshLayout) view_wait.findViewById(R.id.nulldata_homemy);
        nulldata_homemy.setColor(android.R.color.holo_blue_light,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        nulldata_homemy.setMode(LWSwipeRefreshLayout.Mode.BOTH);
        nulldata_homemy.setLoadNoFull(false);
        nulldata_homemy.setOnRefreshListener(this);
        nulldata_homemy.setOnLoadListener(this);
        nulldata_homemy.setRefreshing(true);


        home_alldata_refresh = (LWSwipeRefreshLayout) view_already.findViewById(R.id.home_alldata_refresh);
        home_alldata_refresh.setColor(android.R.color.holo_blue_light,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        home_alldata_refresh.setMode(LWSwipeRefreshLayout.Mode.BOTH);
        home_alldata_refresh.setLoadNoFull(false);
        home_alldata_refresh.setOnRefreshListener(this);
        home_alldata_refresh.setOnLoadListener(this);
        //  dongtai_data_refresh.setRefreshing(true);home_gooddata_refresh home_alldata_refresh

        nulldata_homeother = (LWSwipeRefreshLayout) view_already.findViewById(R.id.nulldata_homeother);
        nulldata_homeother.setColor(android.R.color.holo_blue_light,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        nulldata_homeother.setMode(LWSwipeRefreshLayout.Mode.BOTH);
        nulldata_homeother.setLoadNoFull(false);
        nulldata_homeother.setOnRefreshListener(this);
        nulldata_homeother.setOnLoadListener(this);


    }

    @Override
    public void onLoad() {
        start=start+20;
        if(currIndex==0){
            LWNNetworkCenter center = LWNNetworkCenter.getInstance();
            if(!center.checkNetworkState(activity_self)){
                ToastView_custom toast = new ToastView_custom(activity_self);
                toast.showCustom(activity_self, "检查网络,稍后重试");
                home_gooddata_refresh.setLoading(false);
                return;
            }
            getMyGoodAtListFromNetwork();
        }else{
            getMyAllListFromNetwork();
        }

    }

    @Override
    public void onRefresh() {
        start=0;
        if(currIndex==0){
            LWNNetworkCenter center = LWNNetworkCenter.getInstance();
            if(!center.checkNetworkState(activity_self)){
                ToastView_custom toast = new ToastView_custom(activity_self);
                toast.showCustom(activity_self,  "检查网络,稍后重试");
                home_gooddata_refresh.setRefreshing(false);
//                home_gooddata_refresh.setLoading(false);
                return;
            }else{
                getMyGoodAtListFromNetwork();
            }

        }else{
            getMyAllListFromNetwork();
        }
    }



    /**
     * 初始化动画
     */
    private void InitImageView() {
        /*bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.image_scroll)
                .getWidth();// 获取图片宽度
        System.out.println("bmpW:" + bmpW);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;// 获取分辨率宽度
        System.out.println("screenW:" + screenW);
        offset = screenW/2;// 计算偏移量*/
        Matrix matrix = new Matrix();
        matrix.postTranslate(0, 0);
        cursor.setImageMatrix(matrix);// 设置动画初始位置
    }

    /**
     * 初始化头标
     */
    private void InitTextView() {
        textView_wait.setOnClickListener(new MyOnClickListener(0));
        textView_already.setOnClickListener(new MyOnClickListener(1));
    }

    /**
     * 头标点击监听
     */
    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            if(index == 0){
               textView_wait.setTextColor(getResources().getColor(R.color.theme_color));
               textView_already.setTextColor(getResources().getColor(R.color.color_item_content));
            }
            else{
               textView_already.setTextColor(getResources().getColor(R.color.theme_color));
               textView_wait.setTextColor(getResources().getColor(R.color.color_item_content));

            }
            pagerView_home.setCurrentItem(index);
        }
    }

    /**
     * 初始化ViewPager
     */
    private void InitViewPager() {
        listViews = new ArrayList<View>();
        array_AllmyGoodAt=new ArrayList<LWNModel_myGoodAtList>();
        array_Alltotal=new ArrayList<LWNModel_myGoodAtList>();
        //LayoutInflater mInflater = getLayoutInflater();
        //下面用到view要先初始化view,再add
        view_wait = mInflater.inflate(R.layout.view_home_wait, null);
        view_already = mInflater.inflate(R.layout.view_home_already, null);

        listViews.add(view_wait);
        listViews.add(view_already);

        pagerView_home.setAdapter(new LWNAdapter_home(listViews));
        pagerView_home.setCurrentItem(0);
        pagerView_home.setOnPageChangeListener(new MyOnPageChangeListener());

        //初始化等待回答列表
        listView_wait = (ListView) view_wait.findViewById(R.id.listView_wait);
        listView_wait.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectWaitItem(position);
            }
        });

//        LWNAdapter_myGoodAt adapter_wait = new LWNAdapter_myGoodAt(view_wait.getContext(), R.layout.view_home_wait, array_myGoodAt);
//        listView_wait.setAdapter(adapter_wait);

        //初始化已回答列表
        listView_already = (ListView) view_already.findViewById(R.id.listView_already);
        listView_already.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectAlreadyItem(position);
            }
        });

//        LWNAdapter_myGoodAt adapter_already = new LWNAdapter_myGoodAt(view_already.getContext(), R.layout.view_home_already, array_all);
//        listView_already.setAdapter(adapter_already);
    }

    /**
     * 页卡切换监听
     */
    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {


        //DisplayMetrics dm = new DisplayMetrics();

        //activity_self.getWindowManager().getDefaultDisplay().getMetrics(dm);

        int one = one_two;// 页卡1 -> 页卡2 偏移量

        @Override
        public void onPageSelected(int arg0) {
            Animation animation = new TranslateAnimation(one * currIndex, one * arg0, 0, 0);
            cursor.startAnimation(animation);
            start=0;
            switch (arg0) {
                case 0: {
                    home_gooddata_refresh.setRefreshing(true);
                    LWNNetworkCenter center = LWNNetworkCenter.getInstance();
                    if(!center.checkNetworkState(activity_self)){
                        ToastView_custom toast = new ToastView_custom(activity_self);
                        toast.showCustom(activity_self, "检查网络,稍后重试");
                        home_gooddata_refresh.setRefreshing(false);

                    }else{
                        getMyGoodAtListFromNetwork();
                    }

                    textView_wait.setTextColor(getResources().getColor(R.color.theme_color));
                    textView_already.setTextColor(getResources().getColor(R.color.color_item_content));

                }
                break;
                case 1: {
                    home_alldata_refresh.setRefreshing(true);
                    getMyAllListFromNetwork();
                    textView_already.setTextColor(getResources().getColor(R.color.theme_color));
                    textView_wait.setTextColor(getResources().getColor(R.color.color_item_content));
                    //System.out.println("点击已回答");
                }
                break;
            }

            currIndex = arg0;
            animation.setFillAfter(true);// True:图片停在动画结束位置
            animation.setDuration(300);
            cursor.startAnimation(animation);
        }


        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }
    }


    ///点击ListView
    private void selectWaitItem(int position) {
        Intent intent = new Intent();
        LWNModel_myGoodAtList model_myGoodAtList =array_AllmyGoodAt.get(position);
        Bundle bundle=new Bundle();
        bundle.putString("questionid", model_myGoodAtList.getId());
        intent.putExtras(bundle);
        //intent.putExtra("questionid", model_myGoodAtList.getId());
        intent.setClass(getActivity(), LWNQuestionDetailActivity.class);

        this.startActivity(intent);
        array_AllmyGoodAt.remove(position);
        adapter_wait.notifyDataSetChanged();
    }

    private void selectAlreadyItem(int position) {
        Intent intent = new Intent();
        LWNModel_myGoodAtList model_myGoodAtList =array_Alltotal.get(position);
        Bundle bundle=new Bundle();
        bundle.putString("questionid", model_myGoodAtList.getId());
        intent.putExtras(bundle);
        //intent.putExtra("questionid", model_myGoodAtList.getId());
        intent.setClass(getActivity(), LWNQuestionDetailActivity.class);

        this.startActivity(intent);
        array_Alltotal.remove(position);
        adapter_already.notifyDataSetChanged();
    }

//    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//
//
//    }

    private LWNAdapter_myGoodAt adapter_wait=null;
    private LWNAdapter_myGoodAt adapter_already=null;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 001:
                    home_gooddata_refresh.setVisibility(View.VISIBLE);
                    nulldata_homemy.setVisibility(View.VISIBLE);
                    home_gooddata_refresh.setRefreshing(false);
                    if(adapter_wait==null){
                        if(getActivity()!=null){
                            adapter_wait = new LWNAdapter_myGoodAt(getActivity(), R.layout.view_home_wait, array_AllmyGoodAt);
                            listView_wait.setAdapter(adapter_wait);
                        }

                    }else {
                        adapter_wait.notifyDataSetChanged();
                    }

                    break;
                case 002:
                    home_alldata_refresh.setVisibility(View.VISIBLE);
                    nulldata_homeother.setVisibility(View.VISIBLE);
                    home_alldata_refresh.setRefreshing(false);
                    if(adapter_already==null){
                        adapter_already = new LWNAdapter_myGoodAt(getActivity(), R.layout.view_home_already, array_Alltotal);
                        listView_already.setAdapter(adapter_already);
                    }else {
                        adapter_already.notifyDataSetChanged();
                    }
                    break;
                case 003:

                    adapter_wait.notifyDataSetChanged();
                    home_gooddata_refresh.setLoading(false);

                    break;

                case 004:

                    adapter_already.notifyDataSetChanged();
                    home_alldata_refresh.setLoading(false);
                    break;


            }
        }
    };

    /**
     * 获取首页数据擅长领域
     */
    public void getMyGoodAtListFromNetwork() {
    LWNNetworkCenter center = LWNNetworkCenter.getInstance();
        String string_url = center.getInstance().getMyGoodAtList(start+"", "20", activity_self);
        HttpUtils http = center.getUtils();
        http.configDefaultHttpCacheExpiry(0*0);
        http.send(HttpRequest.HttpMethod.GET,
                string_url,
                new RequestCallBack<Object>() {
                    @Override
                    public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
                        if( home_gooddata_refresh!=null&&home_gooddata_refresh.isRefreshing()){
                            home_gooddata_refresh.setRefreshing(false);
                        }

                        try {
                            JSONObject jsonObject = new JSONObject(objectResponseInfo.result.toString());
                            String string_code = jsonObject.getString("code");
                            String string_msg = jsonObject.getString("msg");

                            switch (String.valueOf(string_code)) {
                                case "10000": {
                                    JSONArray json_array = jsonObject.getJSONArray("data");
                                    array_myGoodAt = LWNJsonToData.getGoodAtListWithJsonString(json_array);
                                    if(start==0){
                                        if(array_AllmyGoodAt!=null&array_AllmyGoodAt.size()!=0){
                                            array_AllmyGoodAt.clear();
                                        }
                                        array_AllmyGoodAt.addAll(array_myGoodAt);
                                        handler.sendEmptyMessage(001);
                                    }else{
                                        array_AllmyGoodAt.addAll(array_myGoodAt);
                                        handler.sendEmptyMessage(003);
                                    }

                                }
                                break;
                                case "10001": {
                                    home_gooddata_refresh.setRefreshing(false);
                                    home_gooddata_refresh.setLoading(false);
//
                                    ToastView_custom toast = new ToastView_custom(activity_self);
                                    toast.showCustom(activity_self,  string_msg);
                                }
                                break;
                                case "10003": {
                                    home_gooddata_refresh.setRefreshing(false);
                                    home_gooddata_refresh.setLoading(false);
                                    String token = LWNModel_user.getInstance().getString_token(activity_self);
                                    if (token != null) {
//
                                        ToastView_custom toast = new ToastView_custom(activity_self);
                                        toast.showCustom(activity_self,  string_msg);
                              if(activity_self.selectId==1){
                                      activity_self.saveIndex(1);
                                       }
                                        Intent intent = new Intent();
                                        intent.putExtra("name", "LeiPei");
                                        intent.setClass(activity_self, LWNLoginActivity.class);
                                        startActivity(intent);
                                        activity_self.finish();
                                    }
                                }

                                break;
                                case "10009":
                                    home_gooddata_refresh.setRefreshing(false);
                                    home_gooddata_refresh.setLoading(false);
                                    JSONArray json_array = jsonObject.optJSONArray("data");
                                    if(start==0&&json_array==null){
                                        nulldata_homemy.setRefreshing(false);
//                                        ToastView_custom toast = new ToastView_custom(activity_self);
//                                        toast.showCustom(activity_self, getResources().getDrawable(R.drawable.error), string_msg);
                                        if (array_AllmyGoodAt != null) {//没有更多
                                            array_AllmyGoodAt.clear();
                                            // handler.sendEmptyMessage(0003);
                                            home_gooddata_refresh.setVisibility(View.GONE);
                                            nulldata_homemy.setVisibility(View.VISIBLE);
                                        }
                                    }else{//没有更多数据
                                        //handler.sendEmptyMessage(0003);

                                        ToastView_custom toast = new ToastView_custom(activity_self);
                                        toast.showCustom(activity_self,  string_msg);
                                    }


                                    break;
                                default: {
                                    home_gooddata_refresh.setRefreshing(false);
                                    home_gooddata_refresh.setLoading(false);
//
                                    ToastView_custom toast = new ToastView_custom(activity_self);
                                    toast.showCustom(activity_self,  string_msg);
                                }
                                break;
                            }

                        } catch (Exception e) {
                            //e.printStackTrace();
                            //proDialog.dismiss();
                        }

                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        if(home_gooddata_refresh!=null){
                            home_gooddata_refresh.setRefreshing(false);
                        }else if(home_gooddata_refresh!=null&&home_gooddata_refresh.isLoading()){
                            home_gooddata_refresh.setLoading(false);
                        }



                    }

                    @Override
                    public void onCancelled() {
                        //proDialog.dismiss();
                    }

                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {

                    }
                }
        );
    }



    /**
     * 获取全部的问题数据
     */
    public void getMyAllListFromNetwork() {
   //     final ProgressDialog proDialog = android.app.ProgressDialog.show(activity_self, "", "加载中，请稍候...");
        LWNNetworkCenter center = LWNNetworkCenter.getInstance();
        if(!center.checkNetworkState(activity_self)){
            ToastView_custom toast = new ToastView_custom(activity_self);
            toast.showCustom(activity_self,  "检查网络,稍后重试");
            home_alldata_refresh.setRefreshing(false);
            home_alldata_refresh.setLoading(false);
            return;
        }
        String string_url = center.getInstance().getAllList(start+"", displaynum+"", activity_self);
        //   System.out.println(string_url);
        HttpUtils http = center.getUtils();
        http.configDefaultHttpCacheExpiry(0*0);
        http.send(HttpRequest.HttpMethod.GET,
                string_url,
                new RequestCallBack<Object>() {
                    @Override
                    public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
                  //      proDialog.dismiss();
                        try {
                            JSONObject jsonObject = new JSONObject(objectResponseInfo.result.toString());
                            String string_code = jsonObject.getString("code");
                            String string_msg = jsonObject.getString("msg");

                            switch (String.valueOf(string_code)) {
                                case "10000": {
                                    JSONArray json_array = jsonObject.getJSONArray("data");
                                    array_all = LWNJsonToData.getGoodAtListWithJsonString(json_array);
                                    if(start==0){
                                        if(array_Alltotal.size()!=0){
                                            array_Alltotal.clear();
                                        }
                                        array_Alltotal.addAll(array_all);
                                        handler.sendEmptyMessage(002);
                                    }else{
                                        array_Alltotal.addAll(array_all);
                                        handler.sendEmptyMessage(004);
                                    }

                                }
                                break;
                                case "10001": {
                                    home_alldata_refresh.setRefreshing(false);
                                    home_alldata_refresh.setLoading(false);
//                                    Toast toast = Toast.makeText(activity_self.getApplicationContext(),
//                                            string_msg, Toast.LENGTH_SHORT);
//                                    toast.setGravity(Gravity.CENTER, 0, 0);
//                                    LinearLayout toastView = (LinearLayout) toast.getView();
//                                    ImageView imageCodeProject = new ImageView(activity_self.getApplicationContext());
//                                    imageCodeProject.setImageResource(R.drawable.error);
//                                    toastView.addView(imageCodeProject, 0);
//                                    toast.show();
                                    ToastView_custom toast = new ToastView_custom(activity_self);
                                    toast.showCustom(activity_self,  string_msg);
                                }
                                break;
                                case "10003": {
                                    home_alldata_refresh.setRefreshing(false);
                                    home_alldata_refresh.setLoading(false);
                                    String token = LWNModel_user.getInstance().getString_token(activity_self);
                                    if (token != null) {
//                                        Toast toast = Toast.makeText(activity_self.getApplicationContext(),
//                                                string_msg, Toast.LENGTH_SHORT);
//                                        toast.setGravity(Gravity.CENTER, 0, 0);
//                                        LinearLayout toastView = (LinearLayout) toast.getView();
//                                        ImageView imageCodeProject = new ImageView(activity_self.getApplicationContext());
//                                        imageCodeProject.setImageResource(R.drawable.error);
//                                        toastView.addView(imageCodeProject, 0);
//                                        toast.show();
                                        ToastView_custom toast = new ToastView_custom(activity_self);
                                        toast.showCustom(activity_self,  string_msg);
                                        if(activity_self.selectId==1){
                                            activity_self.saveIndex(1);
                                        }
                                        Intent intent = new Intent();
                                        intent.putExtra("name", "LeiPei");
                                        intent.setClass(activity_self, LWNLoginActivity.class);
                                        startActivity(intent);
                                        activity_self.finish();
                                    }
                                }
                                break;

                                case "10009":
                                    home_alldata_refresh.setRefreshing(false);
                                    home_alldata_refresh.setLoading(false);

                                    JSONArray json_array = jsonObject.optJSONArray("data");
                                    if(start==0&&json_array==null){
                                        nulldata_homeother.setRefreshing(false);
//                                        ToastView_custom toast = new ToastView_custom(activity_self);
//                                        toast.showCustom(activity_self, getResources().getDrawable(R.drawable.error), string_msg);
                                        if (array_AllmyGoodAt != null) {//没有更多
                                            array_AllmyGoodAt.clear();
                                            // handler.sendEmptyMessage(0003);
                                            home_alldata_refresh.setVisibility(View.GONE);
                                            nulldata_homeother.setVisibility(View.VISIBLE);
                                        }
                                    }else{//没有更多数据

                                        if(array_AllmyGoodAt!=null){
                                            ToastView_custom toast = new ToastView_custom(activity_self);
                                            toast.showCustom(activity_self,  string_msg);
                                        }else{

                                        }

                                    }


                                    break;
                                default: {
                                    home_alldata_refresh.setRefreshing(false);
                                    home_alldata_refresh.setLoading(false);
//                                    Toast toast = Toast.makeText(activity_self.getApplicationContext(),
//                                            string_msg, Toast.LENGTH_SHORT);
//                                    toast.setGravity(Gravity.CENTER, 0, 0);
//                                    LinearLayout toastView = (LinearLayout) toast.getView();
//                                    ImageView imageCodeProject = new ImageView(activity_self.getApplicationContext());
//                                    imageCodeProject.setImageResource(R.drawable.error);
//                                    toastView.addView(imageCodeProject, 0);
//                                    toast.show();
                                    ToastView_custom toast = new ToastView_custom(activity_self);
                                    toast.showCustom(activity_self,  string_msg);
                                }
                                break;
                            }

                        } catch (Exception e) {
                            //e.printStackTrace();
                          //  proDialog.dismiss();
                        }

                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        //System.out.println(s);
                      //  proDialog.dismiss();
                    }

                    @Override
                    public void onCancelled() {
                     //   proDialog.dismiss();
                    }

                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {

                    }
                }
        );
    }

    @Override
    public void onResume() {
        super.onResume();
        start=0;
        if(isResume&&getActivity()!=null){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {Thread.sleep(100);
                        if(currIndex==0){
                            LWNNetworkCenter center = LWNNetworkCenter.getInstance();
                            if(!center.checkNetworkState(activity_self)){
                                ToastView_custom toast = new ToastView_custom(activity_self);
                                toast.showCustom(activity_self, "检查网络,稍后重试");
                                home_alldata_refresh.setRefreshing(false);
                                home_gooddata_refresh.setRefreshing(false);
                                return;
                            }
                            getMyGoodAtListFromNetwork();
                        }else{
                            getMyAllListFromNetwork();
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }



                }
            }).start();

        }

        MobclickAgent.onPageStart("MainScreen");
      //  MobclickAgent.onResume(getActivity());

    }
       private boolean isResume=false;
    @Override
    public void onPause() {
        super.onPause();
        isResume=true;
        MobclickAgent.onPageEnd("MainScreen");
     //   MobclickAgent.onPause(getActivity());
    }
}
